<template>
	<view>
		<div class="import" v-show="loading">
			<van-skeleton title avatar row="15" />
		</div>
		<van-cell-group title="输入学号和密码,可以导入课表哦~" v-show="!loading">
			<van-field :value="stuId" type="number" placeholder="请输入学号" required label="学号" @change="changeStuId" />
			<van-field :value="pswd"  type="password" placeholder="请输入密码" required label="密码" @change="changePSWD" />
		</van-cell-group>
		<div class="importBtn">
			<van-button type="primary" block round color="#006600" :disabled="isNull" @click="importClassTable">导入
			</van-button>
		</div>
			<van-notify id="van-notify" />
	</view>
</template>

<script>
	import Notify from '../../wxcomponents/vant/dist/notify/notify';
	export default {
		data() {
			return {
				loading: false,
				stuId: null,
				pswd: null,
				isNull: true
			}
		},
		beforeCreate() {
			this.loading = true
		},
		mounted() {
			
			this.loading = false
			
		},
		methods: {
			checkNull() {
				if (this.stuId != null && this.pswd != null && this.stuId != "" && this.pswd != "") {
					this.isNull = false
				} else {
					this.isNull = true
				}
			},
			changeStuId(e) {
				this.stuId = e.detail
				this.checkNull()
			},
			changePSWD(e) {
				this.pswd = e.detail
				this.checkNull()
			},
			importClassTable(e) {
				let self = this
				uni.request({
					url: getApp().globalData.baseURL + "/course/importCourse",
					data: {
						"openid": wx.getStorageSync('openid'),
						"username": self.stuId,
						"password": self.pswd
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						if(res.data.code===1){
							Notify({
								type: 'success',
								message: '导入成功'
							});
							
						}else{
							Notify({
								type: 'warning',
								message: '未获取到课表,请检查填写信息是否正确'
							});
						}
					},
					fail: (err) =>{
						Notify({
							type: 'danger',
							message: '导入失败,请检查网络链连接'
						});
					}
				})
			}
		}
	}
</script>

<style scoped>
	.import {
		margin-top: 30rpx;
	}

	.importBtn {
		margin-top: 700rpx;
		text-align: center;
		margin-left: 50rpx;
		margin-right: 50rpx;
	}
</style>
